<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 11:14
 */

namespace App\LogicService;

use App\LogicService\Contracts\BloodEventLogicContract;
use App\Models\Dynamic\BloodDiet;
use App\Models\Dynamic\BloodMedication;
use App\Models\Dynamic\BloodMotion;
use App\Models\Dynamic\SamReferGlucose;

class BloodEventLogic extends BaseLogic implements BloodEventLogicContract
{
    public function getEventList($user_id)
    {
        if ($user_id) {
            //吃饭
            $diet = BloodDiet::whereUserid($user_id)
                ->select(\DB::raw("dietsituation as name, diettime as time ,'吃饭' as type"))
                ->orderBy('diettime', 'desc')
                ->get()
                ->toArray();
            $data = $diet;
            //用药
            $medication = BloodMedication::whereUserid($user_id)
                ->select(\DB::raw("tn_blood_medication_info.name as name,tn_blood_medication.medicationtime as time,'用药' as type"))
                ->join('tn_blood_medication_info', 'tn_blood_medication.id', '=', 'tn_blood_medication_info.medicationid')
                ->whereIn('tn_blood_medication_info.usage', [1, 2])
                ->get()
                ->toArray();
            $data = array_merge($data, $medication);

            //胰岛素
            $medication2 = BloodMedication::whereUserid($user_id)
                ->select(\DB::raw("tn_blood_medication_info.name as name,tn_blood_medication.medicationtime as time,'胰岛素' as type"))
                    ->join('tn_blood_medication_info', 'tn_blood_medication.id', '=', 'tn_blood_medication_info.medicationid')
                    ->whereIn('tn_blood_medication_info.usage', [3, 4])
                    ->get()
                    ->toArray();
            $data = array_merge($data, $medication2);

            //运动
            $motion = BloodMotion::whereUserid($user_id)
                ->select(\DB::raw("motiontime as time ,motiontype as name,durationtime,stepsnum,'运动' as type"))
                ->get()
                ->toArray();

            foreach ($motion as $mk => $mv) {
                if ($mv['name'] != '计步器') {
                    $motion[$mk]['num'] = $mv['durationtime'];
                } else {
                    $motion[$mk]['num'] = $mv['stepsnum'];
                }
            }
            $data = array_merge($data, $motion);

            //参比血糖记录
            $glucose = SamReferGlucose::whereUserid($user_id)
                ->select(\DB::raw("collectedtime as time,value as name,'参比血糖' as type"))
                ->get()
                ->toArray();
            $data = array_merge($data, $glucose);

            $count = count($data);
            for ($i = 0; $i < $count; $i++) {
                for ($j = $count - 1; $j > 0; $j--) {
                    if (strtotime($data[$j]['time']) > strtotime($data[$j - 1]['time'])) {
                        $bak = $data[$j - 1];
                        $data[$j - 1] = $data[$j];
                        $data[$j] = $bak;
                    }
                }
            }

            return $data;
        }
        return false;
    }
}
